import React from 'react';
import { Card, Col, Container, Row, Button, Form } from 'react-bootstrap';
import { Head, router, usePage } from '@inertiajs/react';
import Layout from '../../../Layouts';
import BreadCrumb from '../../../Components/Common/BreadCrumb';
import { FileText, Pencil } from 'lucide-react';
import estilosFoco from '../Contratos/Shared/estilosFocoAccesible.module.css';
import { mapContactoToForm } from './contactoFormUtils';

function tipoViaLabel(contacto: Record<string, unknown> | undefined): string {
    if (!contacto) return '';
    const tv =
        (contacto as any).tipoViaFisica ||
        (contacto as any).tipo_via_fisica ||
        (contacto as any).tipoViaFisica;
    if (!tv || typeof tv !== 'object') return '';
    const ini = (tv as any).IniTipVia ?? '';
    const des = (tv as any).DesTipVia ?? '';
    return [ini, des].filter(Boolean).join(' — ');
}

const VerContacto = () => {
    const { contacto } = usePage().props as { contacto?: Record<string, unknown> };
    const f = React.useMemo(() => mapContactoToForm(contacto), [contacto]);
    const id = Number((contacto as any)?.CodConCli ?? f.CodConCli ?? 0);
    const estado =
        (contacto as any)?.EstConCliLabel ??
        ((contacto as any)?.EstConCli === 1 || (contacto as any)?.EstCli === 1 ? 'ACTIVO' : 'INACTIVO');

    const goEdit = () => {
        try {
            router.visit((route as (n: string, p: object) => string)('contactos.editar', { id }));
        } catch {
            router.visit(`/contactos/${id}/editar`);
        }
    };

    const goBack = () => {
        try {
            router.visit((route as (n: string) => string)('contactos.index'));
        } catch {
            router.visit('/contactos');
        }
    };

    const ro = (v: unknown) => (v == null || v === '' ? '—' : String(v));

    return (
        <React.Fragment>
            <Head title="Ver contacto" />
            <div className="page-content">
                <Container fluid>
                    <BreadCrumb title="Consultar" pageTitle="Contacto" />
                    <Row>
                        <Col xl={12}>
                            <Card>
                                <Card.Header className={`d-flex justify-content-between align-items-center bg-light flex-wrap gap-2 ${estilosFoco.contenedorAccesibleContratos}`}>
                                    <h4 className="card-title mb-0">Contacto — solo lectura</h4>
                                    <div className="d-flex gap-2">
                                        <Button variant="primary" size="sm" onClick={goEdit} aria-label="Editar contacto">
                                            <Pencil size={14} className="me-1" aria-hidden="true" />
                                            Editar
                                        </Button>
                                        <Button variant="secondary" size="sm" onClick={goBack} aria-label="Volver al listado de contactos">
                                            <FileText size={14} className="me-1" aria-hidden="true" />
                                            Volver al listado
                                        </Button>
                                    </div>
                                </Card.Header>
                                <Card.Body>
                                    <Row className="g-3 mb-2">
                                        <Col xs={12}>
                                            <span className="text-muted small">Estado: </span>
                                            <strong>{estado}</strong>
                                        </Col>
                                    </Row>

                                    <h5 className="mb-3">Datos del contacto</h5>
                                    <Row className="g-3 mb-4">
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Nombre</Form.Label>
                                            <div className="fw-medium">{ro(f.NomConCli)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Cargo</Form.Label>
                                            <div>{ro(f.CarConCli)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">NIF / documento</Form.Label>
                                            <div>{ro(f.NIFConCli)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Teléfono fijo</Form.Label>
                                            <div>{ro(f.TelFijConCli)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Teléfono móvil</Form.Label>
                                            <div>{ro(f.TelCelConCli)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Teléfono personal</Form.Label>
                                            <div>{ro(f.TelPersonal)}</div>
                                        </Col>
                                        <Col md={6}>
                                            <Form.Label className="text-muted small mb-0">Email</Form.Label>
                                            <div>{ro(f.EmaConCli)}</div>
                                        </Col>
                                        <Col md={6}>
                                            <Form.Label className="text-muted small mb-0">Email personal</Form.Label>
                                            <div>{ro(f.EmaPersonal)}</div>
                                        </Col>
                                        <Col md={6}>
                                            <Form.Label className="text-muted small mb-0">IBAN</Form.Label>
                                            <div className="text-break">{ro(f.NumIBAN)}</div>
                                        </Col>
                                        <Col md={6}>
                                            <Form.Label className="text-muted small mb-0">Nº colegiado</Form.Label>
                                            <div>{ro(f.NumColegiado)}</div>
                                        </Col>
                                        <Col xs={12}>
                                            <Form.Label className="text-muted small mb-0">Observaciones</Form.Label>
                                            <div className="border rounded p-2 bg-light" style={{ whiteSpace: 'pre-wrap' }}>
                                                {ro(f.Observaciones)}
                                            </div>
                                        </Col>
                                    </Row>

                                    <h5 className="mb-3">Dirección (facturación / envío)</h5>
                                    <Row className="g-3">
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Tipo de vía</Form.Label>
                                            <div>{tipoViaLabel(contacto) || ro(f.CodTipVia)}</div>
                                        </Col>
                                        <Col md={5}>
                                            <Form.Label className="text-muted small mb-0">Nombre de la vía</Form.Label>
                                            <div>{ro(f.NomVia)}</div>
                                        </Col>
                                        <Col md={3}>
                                            <Form.Label className="text-muted small mb-0">Número</Form.Label>
                                            <div>{ro(f.NumVia)}</div>
                                        </Col>
                                        <Col md={3}>
                                            <Form.Label className="text-muted small mb-0">Bloque</Form.Label>
                                            <div>{ro(f.Bloque)}</div>
                                        </Col>
                                        <Col md={3}>
                                            <Form.Label className="text-muted small mb-0">Escalera</Form.Label>
                                            <div>{ro(f.Escalera)}</div>
                                        </Col>
                                        <Col md={3}>
                                            <Form.Label className="text-muted small mb-0">Planta</Form.Label>
                                            <div>{ro(f.Planta)}</div>
                                        </Col>
                                        <Col md={3}>
                                            <Form.Label className="text-muted small mb-0">Puerta</Form.Label>
                                            <div>{ro(f.Puerta)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Código postal</Form.Label>
                                            <div>{ro(f.CodigoPostal)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Provincia</Form.Label>
                                            <div>{ro(f.Provincia)}</div>
                                        </Col>
                                        <Col md={4}>
                                            <Form.Label className="text-muted small mb-0">Localidad</Form.Label>
                                            <div>{ro(f.Localidad)}</div>
                                        </Col>
                                    </Row>
                                </Card.Body>
                            </Card>
                        </Col>
                    </Row>
                </Container>
            </div>
        </React.Fragment>
    );
};

VerContacto.layout = (page: React.ReactNode) => <Layout children={page} />;
export default VerContacto;
