import { __ } from '@wordpress/i18n';
import { Info } from 'lucide-react';
import Toggle from '../components/Toggle';
import DayPills from '../components/DayPills';
import SchemaSections from '../components/SchemaSections';

const SHORT = { 0: 'S', 1: 'M', 2: 'T', 3: 'W', 4: 'T', 5: 'F', 6: 'S' };

const CUTOFFS = Array.from( { length: 24 }, ( _, h ) => {
	const v = String( h ).padStart( 2, '0' ) + ':00';
	const ap = h >= 12 ? 'PM' : 'AM';
	const hr = h % 12 || 12;
	return [ v, `${ hr }:00 ${ ap }${ h === 12 ? ' (noon)' : '' }` ];
} );

export default function GeneralTab( { state, dispatch, accent } ) {
	const s = state.settings;
	const set = ( key, value ) => dispatch( { type: 'SET_FIELD', key, value } );
	const working = Array.isArray( s.working_days ) ? s.working_days.map( Number ) : [ 1, 2, 3, 4, 5 ];
	const fullNames = state.meta?.weekdays;
	const toggleDay = ( d ) =>
		set( 'working_days', working.includes( d ) ? working.filter( ( x ) => x !== d ) : [ ...working, d ].sort() );

	return (
		<>
			<div className="edfw-card edfw-card--row">
				<div style={ { flex: 1 } }>
					<div className="edfw-card__title">{ __( 'Enable delivery estimates', 'wpactpro-estimated-delivery-date' ) }</div>
					<div className="edfw-card__desc">{ __( 'The on/off switch for the whole plugin. When off, no delivery estimate appears anywhere on your store (product, shop, cart, checkout, emails) and no dates are calculated. Use this to pause the plugin without deactivating it.', 'wpactpro-estimated-delivery-date' ) }</div>
				</div>
				<Toggle
					checked={ s.enabled !== 'no' }
					accent={ accent }
					label={ __( 'Enable delivery estimates', 'wpactpro-estimated-delivery-date' ) }
					onChange={ ( v ) => set( 'enabled', v ? 'yes' : 'no' ) }
				/>
			</div>

			<div className="edfw-card">
				<div className="edfw-card__title">{ __( 'Order cutoff', 'wpactpro-estimated-delivery-date' ) }</div>
				<div className="edfw-card__desc" style={ { marginBottom: 16 } }>{ __( 'The daily deadline for same-day dispatch. Orders placed after this time start counting from the next working day, so their estimate moves forward by one day. The delivery window itself (Min/Max days) is set on the Delivery Rules tab.', 'wpactpro-estimated-delivery-date' ) }</div>
				<div style={ { maxWidth: 220 } }>
					<div className="edfw-field-label">{ __( 'Order cutoff time', 'wpactpro-estimated-delivery-date' ) }</div>
					<select className="edfw-select" style={ { width: '100%' } } value={ s.cutoff_time || '14:00' } onChange={ ( e ) => set( 'cutoff_time', e.target.value ) }>
						{ CUTOFFS.map( ( [ v, l ] ) => <option key={ v } value={ v }>{ l }</option> ) }
					</select>
				</div>
				<div className="edfw-info">
					<Info size={ 15 } />
					{ __( 'The cutoff uses your store timezone (WooCommerce → Settings → General).', 'wpactpro-estimated-delivery-date' ) }
				</div>
				<div style={ { display: 'flex', alignItems: 'center', gap: 12, marginTop: 16 } }>
					<Toggle
						checked={ s.countdown_enabled !== 'no' }
						accent={ accent }
						label={ __( 'Show order-within countdown', 'wpactpro-estimated-delivery-date' ) }
						onChange={ ( v ) => set( 'countdown_enabled', v ? 'yes' : 'no' ) }
					/>
					<div>
						<div style={ { fontSize: 14, fontWeight: 600 } }>{ __( 'Show “order within” urgency countdown', 'wpactpro-estimated-delivery-date' ) }</div>
						<div style={ { fontSize: 12.5, color: '#787c82' } }>{ __( 'Adds a live “Order within 2h 36m” timer next to the estimate on product pages to encourage shoppers to order before today’s cutoff. Once the cutoff passes it switches to a “Cutoff passed — ships next business day” note.', 'wpactpro-estimated-delivery-date' ) }</div>
					</div>
				</div>
			</div>

			<div className="edfw-card">
				<div className="edfw-card__title">{ __( 'Working schedule', 'wpactpro-estimated-delivery-date' ) }</div>
				<div className="edfw-card__desc" style={ { marginBottom: 18 } }>{ __( 'The days you actually dispatch orders (highlighted = working). Non-working days — and any active Holidays — are skipped in the calculation, so an order is never estimated to arrive on a day you’re closed. This is how you exclude weekends.', 'wpactpro-estimated-delivery-date' ) }</div>
				<DayPills days={ SHORT } active={ working } onToggle={ toggleDay } fullNames={ fullNames } />
			</div>
			<SchemaSections tab="general" state={ state } dispatch={ dispatch } accent={ accent } />
		</>
	);
}
